<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/15
 * Time: 21:26
 */
//95,100,110
class plugin_xigua_wsq
{

    function global_footer() {
        global $_G;
        $config = $_G['cache']['plugin']['xigua_wsq'];
        $_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
        if($config && $_G['wechat']['setting']['wechat_float_qrcode']) {

            $url = urlencode($this->currenturl_562());

            $TIP = $_G['wechat']['setting']['wechat_float_text'] ? $_G['wechat']['setting']['wechat_float_text'] : '';

            if ($_GET['mod'] != 'post' && $_G['basescript'] != 'userapp' && empty($_G['cookie']['wechatfqrc'])){
               $html = <<<HTML
<div id="wechat_float_qrcode" class="p_pop xg1" style="display:none;text-align:center;float:left;position:fixed;top:220px;z-index:100;margin-left: 2px;width:150px">
    <p class="cl"><img class="y" style="cursor:pointer" onclick="display('wechat_float_qrcode');setcookie('wechatfqrc', 1, 86400)" src="source/plugin/xigua_wsq/static/ad_close.gif"></p>
    <img src="plugin.php?id=xigua_wsq:qrcode&access=yes&url=$url" />
    <p>$TIP</p>
</div>
<script>
    function wechat_qrcode(type) {
        if(type && $('wechat_float_qrcode').style.display == 'none') {
            return;
        }
        var qrleft = parseInt($('ft').clientWidth + parseInt(fetchOffset($('ft'))['left']));
        $('wechat_float_qrcode').style.display = '';
        if(qrleft + $('wechat_float_qrcode').clientWidth > document.documentElement.clientWidth) {
            $('wechat_float_qrcode').style.cssFloat = 'right';
            $('wechat_float_qrcode').style.left = 'auto';
            $('wechat_float_qrcode').style.right = 0;
        } else {
            $('wechat_float_qrcode').style.cssFloat = 'left';
            $('wechat_float_qrcode').style.left = (qrleft) + 'px';
            $('wechat_float_qrcode').style.right = 'auto';
        }
    }
    _attachEvent(window, 'scroll', function () { wechat_qrcode(1); })
    _attachEvent(window, 'load', function() { wechat_qrcode(0); }, document);
</script>
HTML;
            return $html;
            }
        }
    }

    function currenturl_562($related = 0) {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        return $related ? $relate_url : $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }

    public function common()
    {
        global $_G;
        $config = $_G['cache']['plugin']['xigua_wsq'];
        $inwx = strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
        if(!$inwx && $config['onlywx']){
            return ;
        }

        if(
            defined('IN_MOBILE') &&
            !defined('IN_MOBILE_API') &&
            in_array(CURMODULE, array('index', 'guide', 'forumdisplay', 'viewthread', 'space')) &&
            $config['open'] &&
            !$_GET['drewb'] &&
            !$_GET['inajax'] &&
            strtolower($_SERVER['REQUEST_METHOD']) != 'post'
        ){
            switch (CURMODULE){
                case 'index':
                case 'guide':
                    $_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
                    $dindex = intval($_G['wechat']['setting']['wsq_fid']);
                    if($config['myindex'] &&
                        ($config['myindex'] !=$_G['siteurl']) &&
                        ($config['myindex'] !=$_G['siteurl'].'forum.php')
                    ){
                        dheader('location: '.$config['myindex']);
                    }else if($dindex){
                        dheader('location: plugin.php?id=xigua_wsq&a=index&fid='.$dindex);
                    }else if( $_G['setting']['xigua_portal_pid'] ){
                        dheader('Location: '. $_G['siteurl'] . 'plugin.php?id=xigua_portal:index&mobile=no&pid='.$_G['setting']['xigua_portal_pid']);
                    }else{
                        if($_G['cache']['plugin']['xigua_logo']){
                            dheader('Location: '. $_G['siteurl'] . 'plugin.php?id=xigua_logo:forumlist');
                        }else{
                            dheader('Location: '. $_G['siteurl'] . 'plugin.php?id=xigua_wsq');
                        }
                    }
                    break;
                case 'forumdisplay':
                    dheader('location: plugin.php?id=xigua_wsq&a=index&fid='.$_GET['fid']);
                    break;
                case 'viewthread':
                    dheader('location: plugin.php?id=xigua_wsq&a=viewthread&tid='.$_GET['tid']);
                    break;
                case 'space':
                    dheader('location: plugin.php?id=xigua_wsq&a=profile');
                    break;
            }
        }
    }
}

class  mobileplugin_xigua_wsq extends plugin_xigua_wsq{ }